package com.yindian.cos.monitor.config;

import com.yindian.cos.monitor.core.TrackerService;
import com.yindian.cos.monitor.filter.LogFilter;
import com.yindian.cos.monitor.properties.MonitorSpanProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 注册TrackerService和日志过滤器
 *
 * @author zhangchuanhao
 * @create 2018/11/5
 */
@Configuration
public class MonitorConfig {

    private final MonitorSpanProperties monitorSpanProperties;

    @Autowired
    public MonitorConfig(MonitorSpanProperties monitorSpanProperties) {
        this.monitorSpanProperties = monitorSpanProperties;
    }

    @Bean
    public TrackerService trackerService() {
        return new TrackerService(monitorSpanProperties);
    }

    /**
     * 注册过滤器入口
     */
    @Bean
    public FilterRegistrationBean logFilter(TrackerService trackerService) {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new LogFilter(trackerService));
        registration.addUrlPatterns("/*");
        registration.setName("logFilter");
        registration.setOrder(-19999);
        registration.setEnabled(true);
        return registration;
    }
}
